package com.spark.laboratory.controller;

import com.spark.laboratory.pojo.Comments;
import com.spark.laboratory.service.CommentsService;
import com.spark.laboratory.service.UsersService;
import com.spark.laboratory.utils.FengliJsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 状态结果返回 控制器
 * @author  lixizheng
 */
@RestController
public class ResultController {


    private final UsersService usersService;

    private final CommentsService commentsService;

    @Autowired
    public ResultController(CommentsService commentsService, UsersService usersService) {
        this.commentsService = commentsService;
        this.usersService = usersService;
    }

    @RequestMapping("/evaluate/submit")
    public FengliJsonResult submitContext(String content, HttpSession httpSession){
        System.out.println(content);

        Object userId = httpSession.getAttribute("userId");

        if (userId != null){
            Comments comments = new Comments();
            comments.setCommentContent(content);
            comments.setCreateDate(new Date());
            comments.setIsDelete(0);

            comments.setUserId(new Integer(userId.toString()));

            commentsService.saveComment(comments);

            return FengliJsonResult.ok("ok");
        }else{
            return FengliJsonResult.errorMsg("非登录用户不可留言~");
        }
    }
}
